<template>
	<div class="app-container" v-loading="loading" element-loading-text="加载中">
		<el-row :style="{ flexGrow: 1, overflowY: 'auto' }">
			<!-- 中间可滑动的区域 -->
			<OrderUnity :showall=false :isedit=true ref="orderUnity">
			</OrderUnity>
		</el-row>
		<el-row>
			<el-col :span="24" style="position: fixed; bottom: 0; width: 100%; z-index:100;">
				<!-- 底部固定的区域 -->
				<el-form style="background-color: white;">
					<el-button type="danger" @click="handlePost()" style="text-align: center;">发布工单</el-button>
				</el-form>
			</el-col>
		</el-row>
	</div>
</template>

<script>
import Api from '../../api/api'
import OrderUnity from '../../components/orderunity.vue'
export default {
	components: {
		OrderUnity,
	},
	data() {
		return {
			loading:false,
			userList:[]
		}
	},
	methods: {
		onLoad() {
			Api.getUsers().then(response => {
				this.userList = response.data.data
				console.log(response.data)
			})
		},
		handlePost() {
			const formData = this.$refs.orderUnity.getFormData();
			if(formData.customer)
			{
				this.loading = true
				formData.assignee = this.$Global.user._id
				formData.repairman = this.$Global.user._id
				formData.publisher = this.$Global.user._id
				// 使用 formData 对象进行处理
				let mentioned_mobile_list = []
				console.log(this.userList)
				for(var i=0;i<this.userList.length;i++)
				{
					let user = this.userList[i]
					let permission = user.role.role_permission
					console.log(permission)
					console.log("pp",permission&4096)
					if((permission&4096)!=0)
					{
						mentioned_mobile_list.push(user.phone)
					}
				}
				console.log(formData)
				formData.status = 0
				Api.createWorkOrder(formData).then(response => {
					console.log(response.data)
					if (response.status == 200) {
						let content = "你有一个指派任务"+
						"\n客户:"+formData.customer.name+
						"\n地址:"+formData.customer.address+
						"\n联系:"+formData.customer.contact+
						"\n问题:"+formData.problem+
						"\n连接:"+Api.baseWebUrl+"#/pages/tabbar/orders/orders?order_id="+response.data.data._id	
						let message = {
							"msgtype": "text",
							"text": {
								"content": content,
								"mentioned_mobile_list":mentioned_mobile_list
							}
						}
						console.log(message)
						Api.sendGroupMessages(message).then(response => {
							console.log(response.data)
							if (response.status == 200) {
								uni.reLaunch({url: '/pages/tabbar/home/home'})
							}
							this.$message.success(response.data.message);
							this.loading = false
						})
					}
				})
			}else{
				this.$message.success("请选择单位")
			}

		}
	}
}
</script>

<style>
.app-container {
	display: flex;
	flex-direction: column;
	height: 100%;
}

.el-form {
	display: flex;
	justify-content: center;
}

.el-button {
	margin: auto;
}
</style>
